package com.hyt.it.ogt.controller.pj;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.common.config.ApiVersionConstant;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Project;
import com.hyt.it.ogt.pj.model.entity.ProjectSetting;
import com.hyt.it.ogt.pj.model.param.ExamExpertSettingParam;
import com.hyt.it.ogt.pj.model.vo.*;
import com.hyt.it.ogt.pj.service.*;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 评卷项目配置 前端控制器
 * </p>
 *
 * @author liy
 * @since 2022-03-01
 */
@ApiSort(value = 25)
@Api(tags = "25.评卷项目配置接口", value = "评卷项目配置接口")
@RestController
@RequestMapping("/project/setting")
@Slf4j
public class ProjectSettingController extends BaseController {

    @Autowired
    private IProjectSettingService projectSettingService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IExamExpertService examExpertService;

    @Autowired
    private ICommonAutomaticAllocationService commonAutomaticAllocationService;

    @Autowired
    private ISynDataService synDataService;


    @ApiOperationSort(value = 1)
    @ApiOperation(value = "25.1.1根据任务id查询评卷项目配置", response = ProjectSetting.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("query/{projectId}")
    public Object queryProjectSetting(@PathVariable("projectId") String projectId) {
        try {
            ProjectSettingVo projectSettingVo = projectSettingService.queryProjectSettingVo(projectId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(projectSettingVo).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法queryProjectSetting异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }


    @ApiOperationSort(value = 1)
    @ApiOperation(value = "25.1.1切换评卷模式", response = ProjectSetting.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("query/{projectId}/{markModel}")
    public Object queryProjectSetting(@PathVariable("projectId") String projectId, @PathVariable("markModel") String markModel) {
        try {
            ProjectSettingVo projectSettingVo = projectSettingService.queryProjectSettingVo(projectId, markModel);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(projectSettingVo).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法queryProjectSetting异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }


    @ApiOperationSort(value = 2)
    @ApiOperation(value = "25.1.2添加评卷项目配置", response = ProjectSettingVo.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @PostMapping("add")
    public Object add(@RequestBody ProjectSettingVo projectSettingVo) {
        try {
            String message = projectSettingService.checkMarkPublish(projectSettingVo);
            if (!String.valueOf(ResponseCode.SUCCESS).equals(message)) {
                return ApiResponse.builder().code(ResponseCode.ERROR).data(message).build();
            }
            projectSettingService.saveProjectSetting(projectSettingVo);
            Project project = projectService.getById(projectSettingVo.getProjectId());
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(project.getMarkPublish()).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法add异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }

    @ApiOperationSort(value = 3)
    @ApiOperation(value = "25.3.1选择专家", response = ExamExpertSettingVo.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("exam/expert/detail")
    public Object settingExpert(@RequestBody ExamExpertSettingParam examExpertSettingParam) {
        try {
            ExamExpertSettingVo examExpertSettingVo = examExpertService.queryExamExpertSettingVo(examExpertSettingParam);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(examExpertSettingVo).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法settingExpert异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }


    @ApiOperationSort(value = 6)
    @ApiOperation(value = "25.6.1查看地址接口", response = ProjectAddressVo.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("showTwoDimensionalCode/{projectId}")
    public Object showTwoDimensionalCode(@PathVariable("projectId") String projectId) {
        try {
            ProjectAddressVo projectAddressVo = projectSettingService.queryTwoDimensionalCode(projectId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(projectAddressVo).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法showTwoDimensionalCode异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }


    @ApiOperationSort(value = 7)
    @ApiOperation(value = "25.7.1选择专家树", response = ExpertTreeVo.class, notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("getOfficeUserTree")
    public Object getOfficeUserTree() {
        try {
            String deptId = getDeptId();
            List<ExpertTreeVo> expertTreeVos = examExpertService.queryExpertTreeVo(deptId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(expertTreeVos).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法getOfficeUser异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }

    @ApiOperationSort(value = 8)
    @ApiOperation(value = "25.8.1删除专家", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("deleteOfficeUser/{id}")
    public Object deleteOfficeUser(@PathVariable("id") String id) {

        try {
            Integer responseCode = examExpertService.deleteExpertVo(id);
            return ApiResponse.builder().code(responseCode).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法getOfficeUser异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }


    @ApiOperationSort(value = 9)
    @ApiOperation(value = "25.9.1发布", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("publish/{projectId}/{markPublish}")
    public Object publish(@PathVariable("projectId") String projectId, @PathVariable("markPublish") String markPublish) {
        try {
            int publishType = 0;
            if (StringUtils.isNotEmpty(markPublish)) {
                publishType = Integer.valueOf(markPublish);
            }
            Integer responseCode = commonAutomaticAllocationService.commonAutomaticAllocation(projectId, publishType);
            return ApiResponse.builder().code(responseCode).build();
        } catch (Exception e) {
            log.error("ProjectSettingController方法publish异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }


    @ApiOperationSort(value = 9)
    @ApiOperation(value = "25.9.1发布", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("testpublish/{projectId}")
    public Object testpublish(@PathVariable("projectId") String projectId) {
        try {

            Integer responseCode = commonAutomaticAllocationService.commonAutomaticAllocation(projectId);
            return ApiResponse.builder().code(responseCode).build();

        } catch (Exception e) {
            log.error("ProjectSettingController方法publish异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }


    @ApiOperationSort(value = 9)
    @ApiOperation(value = "25.9.1发布", notes = Constants.SWAGGER_METHOD_NOTES)
    @PostMapping("testSynDataToCJ/{taskId}")
    public Object testSynDataToCJ(@PathVariable("taskId") String taskId) {
        try {

            SynDataVo synDataVo = synDataService.synDataToCJ(taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(synDataVo).build();

        } catch (Exception e) {
            log.error("ProjectSettingController方法publish异常:{}", e);
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }

    }

}
